package org.conan.groupapp.system.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.conan.groupapp.framework.action.BaseAction;
import org.conan.groupapp.system.entity.User;
import org.conan.groupapp.system.service.IUserService;
import org.conan.groupapp.util.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandlerImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 用户登录验证，登出acton
 * @author pengjun
 * 
 */
@Controller
public class UserAction extends BaseAction<User>{
	@Autowired
	@Qualifier("userServiceImpl")
	private IUserService iUserService;
	
	
	/**
	 * 用户登录 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/login")
	public String login(HttpServletRequest request,
			HttpServletResponse response) {
		System.out.println("ssss");
		return "login";
	}
	
	@RequestMapping("/index")
	public String index(HttpServletRequest request,
			HttpServletResponse response) {
		System.out.println("index");
		return "index";
	}
	
	/**
	 * 用户登录 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request,
			HttpServletResponse response) {
		request.getSession().removeAttribute(USER_SESSION_NAME);
		return "login";  
	}
	
	

	
	
	/**
	 * 处理AccessDeniedException
	 */
	@RequestMapping("/accessDenied")
	public void accessDenied(ModelMap model,HttpServletRequest request){
		AccessDeniedException exception = (AccessDeniedException)request.getAttribute(AccessDeniedHandlerImpl.SPRING_SECURITY_ACCESS_DENIED_EXCEPTION_KEY);
		model.addAttribute("errorDetails",exception.getMessage());
	}
	
	/**
	 * 处理AuthException
	 */
	@RequestMapping("/authDenied")
	public void authDenied(ModelMap model,HttpServletRequest request,
			HttpServletResponse response){
		ResponseUtils.renderText(response, "authDenied");
	}
	
	/**
	 * 用户列表
	 * @param model
	 * @param request
	 * @param response
	 */
	@RequestMapping("/user/list")
	public String list(HttpServletRequest request,
			HttpServletResponse response,User user){
		if(StringUtils.equalsIgnoreCase(user.getGrid(),"true")){
			String json;
			try {
				json = iUserService.toPageJson(iUserService.pageMapList(user));
			} catch (Exception e) {
				e.printStackTrace();
				json = "error";
			}
			ResponseUtils.renderText(response, json);
			return null;
		}
		return "system/user/index";
	}
	/**
	 * 获取用户菜单
	 * @param model
	 * @param request
	 * @param response
	 */
	@RequestMapping("/user/loadMenu")
	public void loadMenu(ModelMap model,HttpServletRequest request,
			HttpServletResponse response,@RequestParam int node){
		try {
			ResponseUtils.renderText(response, iUserService.loadMenu2Json(node, loadCurrentUser(request)));
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtils.renderText(response,"error");
		}
	}

	public IUserService getiUserService() {
		return iUserService;
	}

	public void setiUserService(IUserService iUserService) {
		this.iUserService = iUserService;
	}
	
	
}
